﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PausePanel : BasePanel
{
    private CanvasGroup cg;
    public AudioSource music;
    public Slider slider1;
    public Slider slider2;
    public Button effectsOnButton;
    public Button effectsOffButton;
    public Button musicOnButton;
    public Button musicOffButton;
    private float tempEffectsVolume;
    private float tempMusicVolume;
    private bool _opening;
    private bool _closing;    private GameObject mainPausePanel;
    // Start is called before the first frame update
    void Awake()
    {
        if (cg == null)
            cg = gameObject.GetComponent<CanvasGroup>();
        music = Camera.main.GetComponent<AudioSource>();
        slider1.value = PlayerPrefs.GetFloat("EffectsVolume"); //音效音量本地读取
        slider2.value = PlayerPrefs.GetFloat("MusicVolume"); //音效音量本地读取
        EffectButtonChoose();
        MusicButtonChoose();
        tempEffectsVolume = 0.3f;
        tempMusicVolume = 0.3f;
        _opening = false;
        _closing = false;
    }
    private void Update()
    {
        if (_opening)
        {
            cg.alpha += Time.unscaledDeltaTime / 0.3f;
            if (cg.alpha >= 1)
                _opening = false;
        }
        if (_closing)
        {
            cg.alpha -= Time.unscaledDeltaTime / 0.3f;
            if (cg.alpha <= 0)
                _closing = false;
        }

    }
    public override void OnEnter()
    {

        if (music == null)
            music = Camera.main.GetComponent<AudioSource>();
        if (cg == null)
            cg = gameObject.GetComponent<CanvasGroup>();
        cg.blocksRaycasts = true;
        cg.interactable = true;

        cg.alpha = 0f;
        _opening = true;
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Pause);
        if (mainPausePanel == null)
        {
            mainPausePanel = GameObject.Find("MainPausePanel(Clone)") as GameObject;
        }
        mainPausePanel.GetComponent<CanvasGroup>().interactable = false;
    }

    public override void OnExit()
    {
        if (cg == null)
            cg = gameObject.GetComponent<CanvasGroup>();
        cg.blocksRaycasts = false;
        cg.interactable = false;

        _closing = true;
        GameManager.Instance.ChangeState(StateOP.Pop);
        if (mainPausePanel == null)
        {
            mainPausePanel = GameObject.Find("MainPausePanel(Clone)") as GameObject;
        }
        mainPausePanel.GetComponent<CanvasGroup>().interactable = true;
    }

    public override void OnPause()
    {
    }

    public override void OnResume()
    {
    }

    public void CloseButton()
    {
        UIManager.Instance.PopPanel();
    }

    public void Button1() //重新开始游戏
    {
        EffectObjectManager.Instance.ButtonEffects();
        if (SaveManager.Instance.HasSaveFile())
        {
            SaveManager.Instance.LoadGame();
        }
        GameManager.Instance.Restart();
    }
    public void Button2() //退出至主菜单
    {
        EffectObjectManager.Instance.ButtonEffects();
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Ready);
    }
    public void Button3()  //退出游戏
    {
        EffectObjectManager.Instance.ButtonEffects();
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
            Application.Quit();
        #endif
    }
    public void Slider1() //音效音量
    {
        PlayerPrefs.SetFloat("EffectsVolume", slider1.value); //音效音量本地存储
        PlayerPrefs.Save();
        EffectButtonChoose();
    }

    public void Slider2() //音乐音量
    {
        music.volume = slider2.value;
        PlayerPrefs.SetFloat("MusicVolume", slider2.value); //音效音量本地存储
        PlayerPrefs.Save();
        MusicButtonChoose();
    }
    public void EffectsOnButton()
    {
        tempEffectsVolume = PlayerPrefs.GetFloat("EffectsVolume");
        PlayerPrefs.SetFloat("EffectsVolume", 0f); //音效音量本地存储
        slider1.value = 0f;
        PlayerPrefs.Save();
        effectsOnButton.gameObject.SetActive(false);
        effectsOffButton.gameObject.SetActive(true);
    }
    public void EffectsOffButton()
    {
        slider1.value = tempEffectsVolume;
        PlayerPrefs.SetFloat("EffectsVolume", tempEffectsVolume); //音效音量本地存储
        PlayerPrefs.Save();
        effectsOnButton.gameObject.SetActive(true);
        effectsOffButton.gameObject.SetActive(false);
    }
    public void MusicOnButton()
    {
        tempMusicVolume = PlayerPrefs.GetFloat("MusicVolume");
        PlayerPrefs.SetFloat("MusicVolume", 0f); //音效音量本地存储
        slider2.value = 0f;
        PlayerPrefs.Save();
        musicOnButton.gameObject.SetActive(false);
        musicOffButton.gameObject.SetActive(true);
    }
    public void MusicOffnButton()
    {
        slider2.value = tempMusicVolume;
        PlayerPrefs.SetFloat("MusicVolume", tempMusicVolume); //音效音量本地存储
        PlayerPrefs.Save();
        musicOnButton.gameObject.SetActive(true);
        musicOffButton.gameObject.SetActive(false);
    }

    private void EffectButtonChoose()
    {
        if (slider1.value == 0f)
        {
            effectsOnButton.gameObject.SetActive(false);
            effectsOffButton.gameObject.SetActive(true);
        }
        if (slider1.value > 0f)
        {
            effectsOnButton.gameObject.SetActive(true);
            effectsOffButton.gameObject.SetActive(false);
        }
    }
    private void MusicButtonChoose()
    {
        if (slider2.value == 0f)
        {
            musicOnButton.gameObject.SetActive(false);
            musicOffButton.gameObject.SetActive(true);
        }
        if (slider2.value > 0f)
        {
            musicOnButton.gameObject.SetActive(true);
            musicOffButton.gameObject.SetActive(false);
        }
    }

}
